﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzyThirdLogic.ExtensionMethods;

namespace EzyThirdLogic
{
    public class Partitioner
    {
        FunctionalDependencySetCollection fdSetCollection = new FunctionalDependencySetCollection();

        public FunctionalDependencySetCollection Partition(FunctionalDependencySet fdSet)
        {
            int x = fdSet.FDSet.Count();
            bool[] inserted = new bool[x];
            for (int i = 0; i < x; i++)
                inserted[i] = false;
            FunctionalDependency fd1, fd2;

            for (int i = 0; i < x; i++)
            {
                fd1 = fdSet.FDSet.ElementAt<FunctionalDependency>(i);
                FunctionalDependencySet newFDSet = new FunctionalDependencySet();
                if (!inserted[i])
                {
                    newFDSet.AddFunctionalDependency(fd1);
                    fdSetCollection.AddFunctionalDependencySet(newFDSet);
                    inserted[i] = true;
                }
                for (int j = 0; j < x; j++)
                {
                    fd2 = fdSet.FDSet.ElementAt<FunctionalDependency>(j);
                    if (!inserted[j])
                    {
                        if (fd2.BitDeterminant.IsEquals(fd1.BitDeterminant))
                        {
                            newFDSet.AddFunctionalDependency(fd2);
                            inserted[j] = true;
                        }
                    }
                }
            }
            return fdSetCollection;
        }
    }
}
